package sis.searchtest;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-08-02 17:30
 * @notify
 * @version 1.0
 */

import junit.framework.TestCase;
import sis.search.ResultsListener;
import sis.search.Search;
import sis.search.SearchScheduler;
import sis.util.LineWriter;
import sis.util.TestUtil;

public class SearchSchedulerTest extends TestCase {
    //搜索次数
    private int actualResultsCount = 0;

    //创建搜索文件
    protected void setUp() throws Exception {
        TestUtil.delete(SearchTest.FILE);
        LineWriter.write(SearchTest.FILE, SearchTest.TEST_HTML);
    }

    //删除搜索文件
    protected void tearDown() throws Exception {
        TestUtil.delete(SearchTest.FILE);
    }

    public void testRepeatedSearch() throws Exception {
        final int searchInterval = 3000;
        //创建搜索
        Search search = new Search(SearchTest.URL, "xxx");
        //搜索回调
        ResultsListener listener = new ResultsListener() {
            public void executed(Search search) {
                ++actualResultsCount;
            }
        };

        //任务调度器
        SearchScheduler scheduler = new SearchScheduler(listener);
        //执行
        scheduler.repeat(search, searchInterval);

        final int expectedResultsCount = 3;
        Thread.sleep((expectedResultsCount - 1) * searchInterval + 1000);

        scheduler.stop();
        assertEquals(expectedResultsCount,actualResultsCount);
    }

}
